class ExperienceController {

  def index = { redirect(action: list, params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [delete: 'POST', save: 'POST', update: 'POST']

  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    def baby = Baby.get(params.id)
    if(!baby) return render(view:'/error')
    def experienceInstanceList = Experience.findAllByBaby(baby, params)
    def experienceInstanceTotal =  Experience.countByBaby(baby)
    [baby:baby, experienceInstanceList: experienceInstanceList, experienceInstanceTotal: Experience.count()]
  }

  def show = {
    def experienceInstance = Experience.get(params.id)

    if (!experienceInstance) {
      flash.message = "Experience not found with id ${params.id}"
      render(view:'/notfound')
    }
    else { return [experienceInstance: experienceInstance] }
  }

  def ajaxdelete = {
    //todo: add permision controll
    def experienceInstance = Experience.get(params.id)
    if (experienceInstance) {
      try {
        experienceInstance.delete()
        render "1"
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        render "0"
      }
    }
    else {
      render "-2"
    }    
  }
  
  def delete = {
    def experienceInstance = Experience.get(params.id)
    if (experienceInstance) {
      try {
        experienceInstance.delete()
        flash.message = "Experience ${params.id} deleted"
        redirect(action: list)
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "Experience ${params.id} could not be deleted"
        redirect(action: show, id: params.id)
      }
    }
    else {
      flash.message = "Experience not found with id ${params.id}"
      redirect(action: list)
    }
  }

  def edit = {
    def experienceInstance = Experience.get(params.id)

    if (!experienceInstance) {
      flash.message = "Experience not found with id ${params.id}"
      redirect(action: list)
    }
    else {
      return [experienceInstance: experienceInstance]
    }
  }

  def update = {
    def experienceInstance = Experience.get(params.id)
    if (experienceInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (experienceInstance.version > version) {

          experienceInstance.errors.rejectValue("version", "experience.optimistic.locking.failure", "Another user has updated this Experience while you were editing.")
          render(view: 'edit', model: [experienceInstance: experienceInstance])
          return
        }
      }
      experienceInstance.properties = params
      if (!experienceInstance.hasErrors() && experienceInstance.save()) {
        flash.message = "Experience ${params.id} updated"
        redirect(action: show, id: experienceInstance.id)
      }
      else {
        render(view: 'edit', model: [experienceInstance: experienceInstance])
      }
    }
    else {
      flash.message = "Experience not found with id ${params.id}"
      redirect(action: edit, id: params.id)
    }
  }

  def create = {
    def experienceInstance = new Experience()
    experienceInstance.properties = params
    return ['experienceInstance': experienceInstance]
  }

  def babyService
  def save = {
    def experienceInstance = new Experience(params)
    //todo: should delete
    if (!experienceInstance.baby) {
      experienceInstance.baby = Baby.get(1)
    }
    def stage = babyService.getStage(experienceInstance.baby)
    experienceInstance.stage = stage
    if (!experienceInstance.hasErrors() && experienceInstance.save()) {
      def ev = new Event(
              baby: experienceInstance.baby,
              title: experienceInstance.title,
              eventType: Event.EVENT_TYPE_POST_EXPERIENCE,
              param1:experienceInstance.id.toString()
      )
      def content = experienceInstance.content
      if(content){
        content = content.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "")
        content = content.replaceAll("[(/>)<]", "");
        if(content.length() > 20) ev.content = content.substring(0, 20) + '...'
        else ev.content = content
      }

      ev.save()
      render('1')
    }
    else {
      println experienceInstance.errors
      render("error:" + experienceInstance.errors)
    }
  }
}
